/*
 * Copyright (C) 2024. GWM Automotive Inc.
 *
 * The copyright notice above does not evidence any actual
 * or intended publication of such source code.
 * The code contains GWM Confidential Proprietary Information.
 */

package com.woparker.kiosk.utils

import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions

/**
 * 获取Glide请求配置
 */
fun getRequestOptions(): RequestOptions {
    return RequestOptions()
        .diskCacheStrategy(DiskCacheStrategy.ALL) // 使用所有缓存
        .skipMemoryCache(false)
}


/**
 * 通过Url加载图片
 */
fun loadImageByUrl(imageView: ImageView, url: String?) {
    loadImageByUrl(imageView, url, null, null)
}

/**
 * 通过Url加载图片
 */
fun loadImageByUrl(
    imageView: ImageView,
    url: String?,
    placeholder: Drawable?,
    requestListener: RequestListener<Drawable>?
) {
    val requestBuilder = Glide.with(imageView.context)
        .load(url)
    placeholder?.let {
        requestBuilder.placeholder(placeholder)
    }
    requestListener?.let {
        requestBuilder.listener(it)
    }
    requestBuilder.apply(getRequestOptions())
        .into(imageView)
}

/**
 * 通过Drawable加载图片
 */
fun loadImageByDrawable(imageView: ImageView, drawable: Drawable) {
    Glide.with(imageView.context)
        .load(drawable)
        .apply(getRequestOptions())
        .into(imageView)
}
